home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / inventor / OpenInventorLab / labSolutions / walk2.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.9 KB  |  143 lines

  1. //-----------------------------------------------------------------------
  2. //
  3. //  SOLUTION: Inventor Barcelona Lab #2
  4. //
  5. //    This program illustrates a simple Inventor program which reads
  6. //    in an Inventor file and views it with the `Walkthrough viewer'.
  7. //    If the `Table' is picked then play some audio...
  8. //
  9. //    EXERCISE:
  10. //    Modify the program so that picking the `Lamp' causes
  11. //      ./sounds/lampON.aiff  and ./sounds/lampOFF.aiff to play.
  12. //      Alternate these two sounds each time.
  13. //
  14. //     (Hint: you will need to create a boolean flag for the lamp status...)
  15. //
  16. //    Type: make walk2
  17. //
  18. //-----------------------------------------------------------------------
  19.  
  20. #include <stdio.h>
  21. #include <Inventor/SoDB.h>
  22. #include <Inventor/actions/SoSearchAction.h>
  23. #include <Inventor/events/SoMouseButtonEvent.h>
  24. #include <Inventor/nodes/SoEventCallback.h>
  25. #include <Inventor/nodes/SoSeparator.h>
  26. #include <Inventor/Xt/SoXt.h>
  27. #include <Inventor/Xt/viewers/SoXtWalkViewer.h>
  28.  
  29.  
  30. int    lampSwitch = 0;
  31.  
  32. void lampCallback( void *, SoEventCallback *cb ) {
  33.     // Make sure that this is a MOUSE PRESS event
  34.     if ( !SO_MOUSE_PRESS_EVENT( cb->getEvent(), ANY )) return;
  35.  
  36.     if ( lampSwitch == 0 ) {
  37.     // Lamp is OFF - turn it ON...
  38.     system( "playaiff sounds/lampON.aiff &" );
  39.     lampSwitch = 1;
  40.     }
  41.     else {
  42.     // Lamp is ON - turn it OFF...
  43.     system( "playaiff sounds/lampOFF.aiff &" );
  44.     lampSwitch = 0;
  45.     }
  46. }
  47.  
  48. void lampSwitchInit( SoSeparator *root )
  49.     // Find the `Lamp' and the `Lamp Switch' nods.
  50.     SoGroup *lamp = (SoGroup *) root->getByName( "Lamp" );
  51.  
  52.     if ( (lamp == NULL) || (!lamp->isOfType( SoGroup::getClassTypeId())) ) {
  53.     printf( "DEBUG: `Lamp' or `LampSwitch' not found, or wrong types\n" );
  54.     exit( 0 );
  55.     }
  56.  
  57.     // Get the path to `Lamp'
  58.     SoSearchAction sa;
  59.     sa.setFind( SoSearchAction::NODE );
  60.     sa.setNode( lamp );
  61.     sa.apply( root );
  62.     SoPath *path = sa.getPath();
  63.  
  64.     // Create an event callback node that calls a function if the
  65.     // `Lamp' is picked.
  66.  
  67.     SoEventCallback *lampCB = new SoEventCallback;
  68.     lampCB->setPath(path);
  69.     lampCB->addEventCallback( SoMouseButtonEvent::getClassTypeId(),
  70.                 lampCallback );
  71.     lamp->addChild( lampCB );
  72. }
  73.  
  74.  
  75. void tableCallback( void *, SoEventCallback *cb ) {
  76.     // Make sure that this is a MOUSE PRESS event
  77.     if ( !SO_MOUSE_PRESS_EVENT( cb->getEvent(), ANY )) return;
  78.  
  79.     system( "playaiff sounds/table.aiff &" );
  80. }
  81.  
  82.  
  83. void tableInit( SoSeparator *root )
  84.     // Find the `Table' node
  85.     SoGroup *table = (SoGroup *) root->getByName( "Table" );
  86.  
  87.     if ( (table == NULL) || (!table->isOfType( SoGroup::getClassTypeId())) ) {
  88.     printf( "DEBUG: `Table' not found or wrong type\n" );
  89.     exit( 0 );
  90.     }
  91.  
  92.     // Get the path to `Table'
  93.     SoSearchAction sa;
  94.     sa.setFind( SoSearchAction::NODE );
  95.     sa.setNode( table );
  96.     sa.apply( root );
  97.     SoPath *path = sa.getPath();
  98.  
  99.     // Create an event callback node that calls a function if the
  100.     // `Table' is picked.
  101.  
  102.     SoEventCallback *tableCB = new SoEventCallback;
  103.     tableCB->setPath(path);
  104.     tableCB->addEventCallback( SoMouseButtonEvent::getClassTypeId(),
  105.                 tableCallback );
  106.     table->addChild( tableCB );
  107. }
  108.  
  109.  
  110. main(int argc, char **argv)
  111. {
  112.     // Initialize Inventor
  113.     Widget myWindow = SoXt::init( argv[0] );
  114.     if (myWindow == NULL) exit(1);
  115.  
  116.     // Make a viewer part of the window
  117.     SoXtWalkViewer *viewer = new SoXtWalkViewer(myWindow);
  118.  
  119.     // Read the object from a file
  120.     SoInput myInput;
  121.     if (!myInput.openFile("./scene.iv")) return(1);
  122.     SoSeparator *scene = SoDB::readAll(&myInput);
  123.     if (scene == NULL) {
  124.     printf( "Error: scene.iv file read failed!\n" );
  125.     exit(1);
  126.     }
  127.     scene->ref();
  128.  
  129.     // Search for the different objects by name
  130.     lampSwitchInit( scene );
  131.     tableInit( scene );
  132.  
  133.     // Viewer setup
  134.     viewer->setSceneGraph( scene );
  135.     viewer->setTitle( "Walkthrough Program" );
  136.     viewer->show();
  137.     SoXt::show(myWindow);
  138.  
  139.     SoXt::mainLoop();
  140. }
  141.